package org.example.system;

import org.example.model.MachineState.IdleState;
import org.example.model.MachineState.MachineState;
import org.example.model.Observable.*;
import org.example.model.PathStrategy.AvoidObstacleStrategy;
import org.example.model.PathStrategy.NormalPathStrategy;
import org.example.model.PathStrategy.PathStrategy;
import org.example.other.FieldData;
import org.example.other.Path;
import org.example.other.SensorData;

// 农机设备基类
public abstract class FarmMachine implements Observer {
    protected String id;
    protected MachineState state;
    protected PathStrategy pathStrategy;
    protected double fuelLevel = 100.0;
    public String position = "未知位置";

    public FarmMachine(String id) {
        this.id = id;
        this.state = new IdleState();
        this.pathStrategy = new NormalPathStrategy();
    }

    public String getId() {
        return id;
    }

    public void setState(MachineState state) {
        this.state = state;
        state.execute(this);
    }

    public void setPathStrategy(PathStrategy strategy) {
        this.pathStrategy = strategy;
    }

    public void planPath(FieldData data) {
        Path path = pathStrategy.calculatePath(data);
        System.out.println("[" + id + "] 规划路径: " + path.getDescription());
    }

    public void update(SensorData data) {
        System.out.println("[" + id + "] 收到传感器更新: " + data);
        // 根据传感器数据调整行为
        if (data.getType().equals("土壤湿度") && data.getValue() > 80) {
            System.out.println("[" + id + "] 土壤湿度过高，调整作业策略");
            setPathStrategy(new AvoidObstacleStrategy());
        }
    }

    public abstract void startTask(String task);
    public abstract void stopTask();

    public void displayStatus() {
        System.out.println("\n设备状态报告 [" + id + "]");
        System.out.println("位置: " + position);
        System.out.println("油量: " + fuelLevel + "%");
        state.execute(this);
    }
}
